<!-- subpkg_consult/patient/index.vue -->
<script setup>
	import { computed, ref } from 'vue'
	import { patientListAPI } from '@/apis/user'
	import { onShow } from '@dcloudio/uni-app'
	import { useConsultStore } from '@/stores/index.js'
	// 侧滑按钮配置
	const swipeOptions = ref([
		{
			text: '删除',
			style: {
				backgroundColor: '#dd524d'
			}
		}
	])

	// 获取患者的信息
	// 保存患者列表
	const patientList = ref([])
	const patientShow = ref(false)
	const getPatientList = async () => {
		const res = await patientListAPI()
		patientShow.value = true
		patientList.value = res.data
	}

	const activeIndex = ref(0)
	const choosePatient = (index) => {
		activeIndex.value = index
	}

	// 通过计算属性得到患者id
	const patientId = computed(() => {
		const item = patientList.value.find(
			(item, index) => index === activeIndex.value
		)
		return item.id
	})

	onShow(() => {
		getPatientList()
	})

	const onNext = () => {
		// 判断是否有 patientId
		if (!patientId.value) {
			return uni.showToast({
				icon: 'none',
				title: '请选择患者'
			})
		}
		useConsultStore().setPatientId(patientId.value)
		// 如果有患者id 可以将患者id保存到 
		uni.navigateTo({
			url: `/subpkg_consult/payment/index`
		})
	}
</script>
<template>
	<scroll-page>
		<view class="patient-page" v-if="patientShow">
			<view class="page-header">
				<view class="patient-title"> 请选择患者信息 </view>
				<view class="patient-tips">
					以便医生给出更准确的治疗，信息仅医生可见
				</view>
			</view>
			<uni-swipe-action>
				<uni-swipe-action-item
					:right-options="swipeOptions"
					v-for="(item, index) in patientList"
					:key="item.id"
				>
					<view
						class="archive-card"
						:class="{ active: activeIndex === index }"
						@click="choosePatient(index)"
					>
						<view class="archive-info">
							<text class="name">{{ item.name }}</text>
							<text class="id-card">
								{{
									item.idCard.replace(/^(\d{6})\d{8}(\d{4})$/, '$1********$2')
								}}
							</text>
							<text class="default" v-if="item.defaultFlag === 1">默认</text>
						</view>
						<view class="archive-info">
							<text class="gender">{{ item.genderValue }}</text>
							<text class="age">{{ item.age }}岁</text>
						</view>
						<navigator
							hover-class="none"
							class="edit-link"
							url="/subpkg_archive/form/index"
						>
							<uni-icons
								type="icon-edit"
								size="20"
								color="#16C2A3"
								custom-prefix="iconfont"
							/>
						</navigator>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>

			<!-- 添加按钮 -->
			<view v-if="true" class="archive-card">
				<navigator
					class="add-link"
					hover-class="none"
					url="/subpkg_archive/form/index"
				>
					<uni-icons color="#16C2A3" size="24" type="plusempty" />
					<text class="label">添加患者</text>
				</navigator>
			</view>
		</view>
		<!-- 下一步操作 -->
		<view class="next-step">
			<button class="uni-button" @click="onNext">下一步</button>
		</view>
	</scroll-page>
</template>

<style lang="scss">
	@import './index.scss';
</style>
